package com.xxjh.brs.uavmodeling.constants;

import com.xxjh.brs.sftp.vo.FileVo;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author CHENCHEN
 * @title 无人机建模推送常量类
 * @company 新兴际华科技（天津）有限公司
 * @create 2024-07-03 19:21
 */
public class UavModelingConstant {

    // 3d 路径
    public static final String TILES_3D = "3d/";

    public static final String HASH_FIELD_STATUS = "status";

    public static final String TASKID = "taskId";

    public static final String HASH_FIELD_MESSAGE = "message";

    public static final String FLODERNAME = "floderName";

    public static final String UPDATETIME = "updateTime";

    // 任务ID集合的RedisKEY
    public static final String TASK_IDS_SET = "TaskIdsSet";

    // 所有的3DTiles路径集合KEY
    public static final String ALL_TILES_KEY = "ALL_TILES_KEY";

    // 所有的已完成3DTiles文件夹名称集合SET
    public static final String ALL_DIRECTORY_SET = "ALL_DIRECTORY_SET";

    // 任务ID集合的RedisKEY
    private static final String TASK_DIRECTORY_KEY = "TASK_DIRECTORY_KEY";

    // 任务状态：运行中
    public static final String STATUS_RUNNING = "RUNNING";

    // 任务状态：重新加载
    public static final String STATUS_RELOADING = "RELOADING";

    // 任务状态：已结束
    public static final String STATUS_FINISHED = "FINISHED";

    //目录名称
    public static final String DIRECTORY = "DIRECTORY";

    //任务ID
    public static final String ID = "ID";

    public static final String[] FOLDERSTOCHECK = {"3dTiles", "dense_b3dm", "sparse_b3dm"};

    public static final String PRE_TILES = "^(.+)/datasets/project/";  // 路径前缀

    public static final String PRE_DIRECTORY = "(3d/.+?)/(\\d+)/datasets/project/";   // 路径前缀

    public static final String TILES = "/3dTiles/";

    public static final String DENSE_B3DM = "/PointClouds/dense_b3dm/";

    public static final String SPARSE_B3DM = "/PointClouds/sparse_b3dm/";

    public static final String TILESET_JSON = "tileset.json";

    /**
     * 通过地图ID生成任务唯一标识。
     *
     * @param directory 目录名称
     * @return 地图ID唯一标识的KEY。
     */
    public static String getTaskDirectoryIdKey(String directory) {
        return TASK_DIRECTORY_KEY.replace(DIRECTORY, directory);
    }

    /**
     * 获取文件夹名称，去除路径中前缀和后缀。
     *
     * @param paths 待筛选和排序的原始文件路径列表。
     * @return 经过优先级筛选后的文件夹名称。
     */
    public static List<FileVo> directoryNameFilter(List<FileVo> paths) {
        Pattern pattern = Pattern.compile(PRE_DIRECTORY);
        List<FileVo> list = new ArrayList<>();
        for (FileVo fileVo : paths) {
            Matcher matcher = pattern.matcher(fileVo.getFilePath());
            if (matcher.find()) {
                String desiredPrefix = matcher.group(1); // 提取3d后面直到1/datasets/project/前的部分
                if(desiredPrefix != null){
                    list.add(new FileVo(desiredPrefix.split("/")[1]));
                }
            }
        }
        return list;
    }
}
